package cn.yuanqiao.archive.report.config;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.jmreport.api.JmReportTokenServiceI;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 自定义积木报表鉴权(如果不进行自定义，则所有请求不做权限控制)
 * 1.自定义获取登录token
 * 2.自定义获取登录用户
 */
@Component
public class JimuReportTokenService implements JmReportTokenServiceI {

    /**
     * 通过请求获取Token
     * @param request
     * @return
     */
    @Override
    public String getToken(HttpServletRequest request) {
        Map jsonmap=new HashMap();
        jsonmap.put("UserID", request.getSession().getAttribute("UserID"));
        jsonmap.put("UserName", request.getSession().getAttribute("UserName"));
        jsonmap.put("tenantId", request.getSession().getAttribute("tenantId"));
        JSONObject json = new JSONObject(jsonmap);
        sun.misc.BASE64Encoder base=new sun.misc.BASE64Encoder();
        //指定utf-8编码
        byte[] bytes = json.toString().getBytes();
        //对数组进行加密
        String encode = base.encode(bytes);
        return encode;
    }

    /**
     * 通过Token获取登录人用户名
     * @param token
     * @return
     */
    @Override
    public String getUsername(String token) {
        // return JwtUtil.getUsername(token);
        sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
        byte[] buffer = new byte[0];
        try {
            buffer = decoder.decodeBuffer(token);
        } catch (IOException e) {
            System.out.println("token解析失败");
            e.printStackTrace();
        }
        //指定utf-8解码
        String jsonString=new String(buffer);
        JSONObject json=JSONObject.parseObject(jsonString);
        return json.getString("UserName");
    }

    /**
     * Token校验
     * @param token
     * @return
     */
    @Override
    public Boolean verifyToken(String token) {
        sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
        byte[] buffer = new byte[0];
        try {
            buffer = decoder.decodeBuffer(token);
        } catch (IOException e) {
            System.out.println("token解析失败");
            e.printStackTrace();
        }
        //指定utf-8解码
        String jsonString=new String(buffer);
        JSONObject json=JSONObject.parseObject(jsonString);
        if("".equals(json.getString("UserID"))||null==json.getString("UserID")){
            return false;
        }
        return true;
    }

    /**
     *  自定义请求头
     * @return
     */
    @Override
    public HttpHeaders customApiHeader() {
        HttpHeaders header = new HttpHeaders();
        header.add("custom-header1", "Please set a custom value 1");
        header.add("token", "token value 2");
        return header;
    }
    /**
     * 获取多租户id
     * @return tenantId
     */
    public String getTenantId() {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = servletRequestAttributes.getRequest();
        HttpServletResponse response = servletRequestAttributes.getResponse();
        String tenantId = String.valueOf(request.getSession().getAttribute("tenantId"));
        if(StringUtils.isNotEmpty(tenantId)){
            response.setHeader("tenantId",tenantId);
            return tenantId;
        }
        return "";
    }
}